package com.dragonfly.shutter.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.beans.PagedBean;
import com.dragonfly.shutter.services.FriendListService;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 好友列表控制器
 * @author 黄挺
 *
 */
@Controller
@RequestMapping("/friendList.html")
public class FriendListController {
	@Autowired
	FriendListService friendListService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session, HttpServletRequest request,
			@RequestParam(value="page", required=false) String pageStr){
		Message message = null;
		Integer userID = (Integer) session.getAttribute("userID");
		
		//尚未登录的用户
		if(userID == null ){
			message = new Message("您尚未登录，请先登录", "未登录", "login.html");
			return new ModelAndView("message", "message", message);
		}
		
		//空的参数
		if(pageStr == null ){
			message = new Message("空的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pageStr)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		Integer page = Integer.parseInt(pageStr);
		
		PagedBean friends = friendListService.doService(userID, page, request.getRequestURL().toString());
		return new ModelAndView("friendList", "friends", friends);
	}
}
